package cl.jpavlich.mpm.view;

import java.awt.BorderLayout;
import java.util.ResourceBundle;

import javax.swing.JDialog;

import org.metawidget.inspector.annotation.UiAction;
import org.metawidget.swing.SwingMetawidget;
import org.metawidget.swing.layout.FlowLayout;

import cl.jpavlich.mpm.services.Bundle;


public class EditDialog extends JDialog {

	private static final long serialVersionUID = 7988774964891350069L;

	private SwingMetawidget mwButtons;

	private SwingMetawidget mwObj;

	public EditDialog() {
		super();
		setModal(true);
		mwObj = GUIUtils.createMetaWidget(false);
		getContentPane().add(mwObj, BorderLayout.CENTER);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setSize(400, 250);
		mwButtons = GUIUtils.createMetaWidget(false);
		mwButtons.setConfig("cl/jpavlich/mpm/view/config/metawidget.xml");
		mwButtons.setMetawidgetLayout(new FlowLayout());
		getContentPane().add(mwButtons,BorderLayout.SOUTH);
		GUIUtils.center(this);
	}

	public void edit(Object obj) {
		ResourceBundle b = Bundle.getInstance();
		setTitle(b.getString(obj.getClass().getSimpleName()));
		mwObj.setToInspect(obj);
		mwButtons.setToInspect(this);
		setVisible(true);
		
	}

	@UiAction
	public void doneEditing() {
		GUIUtils.save(mwObj);
		setVisible(false);
	}

}
